package com.lbs.droiddoctor;


import com.lbs.droiddoctor.base.BaseActivity;
import com.lbs.droiddoctor.common.HttpASync;
import com.lbs.droiddoctor.common.HttpASync.OnRequest;
import com.lbs.droiddoctor.constants.AppConstants;
import com.lbs.droiddoctor.home.HomeScreen;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class LoginActivity extends BaseActivity implements OnClickListener,OnRequest {
private Button login;
private EditText userName;
private EditText password;
private Button cancel;
private TextView register;
private Context ctx;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ctx = this;
        login=(Button)findViewById(R.id.login);
        userName=(EditText)findViewById(R.id.username);
        password=(EditText)findViewById(R.id.password);
        cancel=(Button)findViewById(R.id.cancel);
        register = (TextView) findViewById(R.id.register);
        
        login.setOnClickListener(this);
        cancel.setOnClickListener(this);
        register.setOnClickListener(this);
        
        
        
    }

	@Override
	public void onClick(View v) {
		
	if(v.getId()==R.id.login){
		String uname=userName.getText().toString();
		String pwd=password.getText().toString();
		if(uname.equals("")&&pwd.equals("")){
			Toast.makeText(this,"Please enter username and password", Toast.LENGTH_SHORT).show();
		}else {
			String loginUrl = AppConstants.LOGIN_PATH+"?Username="+uname+"&Password="+pwd;
			SharedPreferences pref = getSharedPreferences(AppConstants.PREF_FILE,0);
			SharedPreferences.Editor editor = pref.edit();
			editor.putString("username", uname);
			editor.commit();
			HttpASync request  = new HttpASync();
			request.setOnRequestListener(this);
			request.execute(loginUrl);
			showProgress("Authenticating, please wait");
		}
		/*if(uname.equals("admin")&&pwd.equals("admin")){
			Toast.makeText(this,"Login successful", Toast.LENGTH_SHORT).show();
			Intent in = new Intent(this, HomeScreen.class);
			startActivity(in);
			finish();
		} else {
			Toast.makeText(this,"Login unsuccessful", Toast.LENGTH_SHORT).show();
		}*/
			
	}else if(v.getId()==R.id.cancel){
		finish();
	} else if(v.getId()==R.id.register) {
		Intent intent = new Intent(this, RegisterActivity.class);
		startActivity(intent);
	}
		
	}

	@Override
	public void onResponseReceived(String response) {
		if(response!=null&&response.contains("True")) {
			
			runOnUiThread(new Runnable() {
				
				@Override
				public void run() {
					Toast.makeText(ctx,"Login successful", Toast.LENGTH_SHORT).show();
					Intent in = new Intent(ctx, HomeScreen.class);
					hideProgress();
					startActivity(in);
					finish();
					
				}
			});
		} else {
			runOnUiThread(new Runnable() {
				
				@Override
				public void run() {
					Toast.makeText(ctx,"Login unsuccessful", Toast.LENGTH_SHORT).show();
					hideProgress();
				}
			});
		}
		
	}
    
	
}
